package com.baizhi.serviceimpl;

import com.baizhi.annotation.AddLog;
import com.baizhi.dao.CategoryMapper;
import com.baizhi.dto.CategoryPageDTO;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonQueryPageVo;
import com.baizhi.vo.CommonVo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public CommonQueryPageVo queryOnePage(PageDTO pageDTO) {
        CategoryExample example = new CategoryExample();

        example.createCriteria().andLevelsEqualTo(1);

        int count = categoryMapper.selectCountByExample(example);

        RowBounds rowBounds = new RowBounds((pageDTO.getPage() - 1) * pageDTO.getPageSize(), pageDTO.getPageSize());

        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);

        return new CommonQueryPageVo(pageDTO.getPage(),count,categoryList);
    }

    @AddLog(value = "添加类别")
    @Override
    public CommonVo add(Category category) {

        try {

            if(category.getParentId()==null){
                category.setLevels(1);
            }else{
                category.setLevels(2);
            }
            category.setId(UUIDUtil.getUUID());

            categoryMapper.insertSelective(category);
            return CommonVo.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVo.faild("修改失败");
        }

    }

    @AddLog(value = "删除类别")
    @Override
    public CommonVo delete(Category category) {
        if(category.getParentId()==null){

            CategoryExample categoryExample = new CategoryExample();
            //查询一级目录下是否有二级目录
            categoryExample.createCriteria().andParentIdEqualTo(category.getId());

            //一级目录下二级目录的数量
            int count = categoryMapper.selectCountByExample(categoryExample);
            if(count==0){
                categoryMapper.delete(category);
                return CommonVo.success("删除一级类别成功");
            }else{
                return CommonVo.faild("删除一级类别失败");
            }

        }else{
            categoryMapper.delete(category);
            return CommonVo.success("删除二级类别成功");

        }
    }

    /*@Override
    public CommonVo delete(Category category) {
        if(category.getParentId()==null){

            CategoryExample categoryExample = new CategoryExample();
            //查询一级目录下是否有二级目录
            categoryExample.createCriteria().andParentIdEqualTo(category.getId());

            //一级目录下二级目录的数量
            int count = categoryMapper.selectCountByExample(categoryExample);
            if(count==0){
                categoryMapper.delete(category);
                return CommonVo.success("删除一级类别成功");
            }else{
                return CommonVo.faild("删除一级类别失败");
            }

        }else{
            categoryMapper.delete(category);
            return CommonVo.success("删除二级类别成功");

        }
    }*/

    @Override
    public Category queryById(String id) {

        return categoryMapper.selectByPrimaryKey(id);
    }

    @AddLog(value = "修改类别")
    @Override
    public CommonVo update(Category category) {
        try {
            categoryMapper.updateByPrimaryKeySelective(category);
            return CommonVo.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVo.faild("修改失败");
        }
    }

    @Override
    public CommonQueryPageVo queryTwoPage(CategoryPageDTO categoryPageDTO) {
        CategoryExample example = new CategoryExample();

        example.createCriteria().andParentIdEqualTo(categoryPageDTO.getCategoryId());

        int count = categoryMapper.selectCountByExample(example);

        RowBounds rowBounds = new RowBounds((categoryPageDTO.getPage() - 1) * categoryPageDTO.getPageSize(), categoryPageDTO.getPageSize());

        List<Category> categoryList = categoryMapper.selectByExampleAndRowBounds(example, rowBounds);

        return new CommonQueryPageVo(categoryPageDTO.getPage(),count,categoryList);
    }

    @Override
    public List<Category> queryByLevelsCategory(Integer levels) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelsEqualTo(levels);

        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        return categoryList;
    }

    @AddLog(value = "删除类别")
    @Override
    public String deletes(Category category) {

        String message=null;
        if(category.getParentId()==null){

            CategoryExample categoryExample = new CategoryExample();
            //查询一级目录下是否有二级目录
            categoryExample.createCriteria().andParentIdEqualTo(category.getId());

            //一级目录下二级目录的数量
            int count = categoryMapper.selectCountByExample(categoryExample);
            if(count==0){
                categoryMapper.delete(category);

                message="删除一级类别成功";
                //return CommonVo.success("删除一级类别成功");
            }else{
                throw new RuntimeException("删除一级类别失败(一级类别下有二级类别)");
                //return CommonVo.faild("删除一级类别失败");
            }

        }else{
            categoryMapper.delete(category);
            message="删除二级类别成功";
            //return CommonVo.success("删除二级类别成功");

        }
        return message;
    }
}
